<template>
	<view>
		<view class="goods-list">
			<!-- block当作包裹的容器 -->
			<view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
				<view class="goods-item">
					<!-- 左边的图片 -->
					<view class="goods-item-left">
						<image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
					</view>
					<!-- 右边的详情和价格 -->
					<view class="good-item-right">
						<!-- 商品的标题 -->
						<view class="goods-name">{{goods.goods_name}}</view>
						<!-- 价格和... -->
						<view class="goods-info-box">
							<view class="goods-price">￥{{goods.goods_price | tofixed}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				queryObj: {
					// 查询关键词
					query: '',
					// 商品分类Id
					cid: '',
					// 页码值
					pagenum: 1,
					// 每页显示多少条数据
					pagesize: 10,

				},
				// 商品列表的数据
				goodsList: [],
				// 总数量，用来实现分页
				total: 0,
				//空白照片
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				 // 是否正在请求数据
				isloading: false
			}
		},
		// 使用过滤器给价格加小数点
		filters: {
			tofixed(num) {
				return Number(num).toFixed(2)
			}
		},
		onLoad(options) {
			console.log(options)
			this.queryObj.query = options.query || ''

			this.queryObj.cid = options.cid || ''

			this.getGoodsList()
		},
		methods: {
			async getGoodsList(cb) {
				
				this.isloading = true
				
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				console.log(res)
				
				this.isloading = false
				
				cb && cb()
				
				if (res.meta.status !== 200) return uni.$showMsg()
				
				
				
				// 储存全部的obj
				this.goodsList = [...this.goodsList, ...res.message.goods]
				// 共多少条数据
				this.total = res.message.total
			},
			gotoDetail(goods){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+ goods.goods_id
				})
			}

		},
		onReachBottom() {
			
			// 判断是否还有下一页数据
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
			  
			if(this.isloading) return
			
			this.queryObj.pagenum++
			//重新获取数据
			this.getGoodsList()
		},
		onPullDownRefresh(){
			this.goodsList= []
			this.total= 0
			this.isloading=false
			this.queryObj.pagenum = 1
			// 重新发起数据请求，然后收回下拉刷新效果
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
	.goods-item {
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid #efefef;

		.goods-item-left {
			padding-right: 5px;

			.goods-pic {
				width: 100px;
				height: 100px;
				display: block;
			}

		}

			// 右边框
		.good-item-right {
			display: flex;
			flex-direction: column;
			// 上下贴边对齐
			justify-content: space-between;

			// 下面是标题
			.goods-name {
				font-size: 13px;
			}

			.goods-info-box {

				// 下面是价格
				.goods-price {
					font-size: 16px;
					color: #c00000;
				}
			}
		}
	}
</style>
